/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package corejavapractice2;

import java.awt.Cursor;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import javax.swing.JComponent;
import javax.swing.JFrame;

/**
 *
 * @author dlao
 */
public class CoreJavaPractice2 {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        EventQueue.invokeLater(new Runnable() {

            public void run() {
                MouseFrame frame = new MouseFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setVisible(true);
            }
        });
    }
}

class MouseFrame extends JFrame {

    public static final int DEFAULT_WIDTH = 300;
    public static final int DEFAULT_HEIGHT = 200;

    public MouseFrame() {
        setTitle("MouseTest");
        setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);

        MouseComponent component = new MouseComponent();
        add(component);
    }
}

class MouseComponent extends JComponent {

    private static final int SIDELENGTH = 10;
    private ArrayList<Rectangle2D> squares;
    private Rectangle2D current;
  
    public MouseComponent() {
        squares = new ArrayList<Rectangle2D>();
        current = null;

        addMouseListener(new MouseHandler());
        addMouseMotionListener(new MouseMotionHandler());
    }

    public void painComponent(Graphics g) {
       Graphics2D g2 = (Graphics2D) g;
      
        for (Rectangle2D r : squares) {
            g2.draw(r);
        }
      
    }

    public Rectangle2D find(Point2D p) {
        for (Rectangle2D r : squares) {
            if (r.contains(p)) {
                return r;
            }

        }
        return null;
    }

  

    public void add(Point2D p) {
        double x = p.getX();
        double y = p.getY();

        current = new Rectangle2D.Double(x - SIDELENGTH / 2, y - SIDELENGTH / 2,
                SIDELENGTH, SIDELENGTH);
      
       
        squares.add(current);
        
         
        repaint();
    }

    public void remove(Rectangle2D s) {
        if (s == null) {
            return;
        }
        if (s == current) {
            current = null;
        }
        squares.remove(s);
        repaint();
    }
    
    
 

    private class MouseHandler extends MouseAdapter {

        public void mousePressed(MouseEvent event) {
            current = find(event.getPoint());
            if (current == null) {
                add(event.getPoint());
          
                System.out.println("mousePressed: " + squares);
            }
        }

        public void mouseClicked(MouseEvent event) {
            current = find(event.getPoint());
            if (current != null && event.getClickCount() >= 2) {
                remove(current);
            }
        }
    }

    private class MouseMotionHandler implements MouseMotionListener {

        public void mouseMoved(MouseEvent event) {
            if (find(event.getPoint()) == null) {
                setCursor(Cursor.getDefaultCursor());
                System.out.println("mouseMove point: " + event.getPoint());
            } else {
                setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
            }
        }

        public void mouseDragged(MouseEvent event) {
            if (current != null) {
                int x = event.getX();
                int y = event.getY();

                current.setFrame(x - SIDELENGTH / 2, y - SIDELENGTH / 2,
                        SIDELENGTH, SIDELENGTH);
                repaint();
            }
        }
    }
}